
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.json.Data;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.TushuoDO;
import kaizone.haining.chaobohui.json.TushuoItem;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.widget.TushuoHead;
import kaizone.haining.chaobohui.widget.UsersActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

public class DetailTushuoFragment extends Fragment implements DataControl.IDataUpdateListener {

    // public static final String DATA_SOURCE_URL =
    // "http://api.dp-app.com/chao/remote/rest/query_tushuo_list.htm?cate=";
    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/query_tushuo_list.htm?cateId=";

    public static final String FLAG_CATE = "cate";

    private ImagePagerAdapter mAdapter;

    private ViewPager mPager;

    private TushuoHead mHead;

    private TextView mBottom;

    private DataControl mDataControl;

    private List<TushuoItem> mData;

    private String mCate;

    private int mId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCate = getArguments() != null ? getArguments().getString(FLAG_CATE) : "";
        mId = getArguments() != null ? getArguments().getInt(TushuoItem.ID) : 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mData = new ArrayList<TushuoItem>();
        View view = inflater.inflate(R.layout.tushuo_detail, null);
        mPager = (ViewPager) view.findViewById(R.id.pager);
        mHead = (TushuoHead) view.findViewById(R.id.title);
        mBottom = (TextView) view.findViewById(R.id.tv02);

        mAdapter = new ImagePagerAdapter(getFragmentManager());
        mPager.setAdapter(mAdapter);
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                updateHead(arg0);
                updateBottom(arg0);
                updateUserActionBar(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);

        return view;
    }

    public void updateHead(int arg0) {
        int count = mData.size();
        int page = arg0 + 1;
        String title = page + "/" + count;
        mHead.setText(title);

        TushuoItem item = mData.get(arg0);

        PicModel picmodel = item.getPicModel();
        if (picmodel == null)
            return;
        mHead.setDownurl(picmodel.url);
    }

    public void updateBottom(int arg0) {
        int count = mData.size();
        if (arg0 > count && arg0 < 0)
            return;
        TushuoItem item = mData.get(arg0);
        String content = item.getContent();
        mBottom.setText(content);
    }

    public void updateUserActionBar(int arg0) {
        int count = mData.size();
        if (arg0 > count && arg0 < 0)
            return;
        TushuoItem item = mData.get(arg0);
        String name = item.getName();
        UsersActionBar.attchShareData(getActivity(), name, item.getPicModelList());
    }

    public void switchShow() {
        if (mHead.isShown()) {
            mHead.setVisibility(View.INVISIBLE);
        } else {
            mHead.setVisibility(View.VISIBLE);
        }

        if (mBottom.isShown()) {
            mBottom.setVisibility(View.INVISIBLE);
        } else {
            mBottom.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataControl.exec(DATA_SOURCE_URL + mId);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        TushuoDO tushuoDO = TushuoDO.parseJson(datastring);
        return tushuoDO;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (result instanceof TushuoDO) {
            TushuoDO tushuoDo = (TushuoDO) result;
            Data data = tushuoDo.getData();
            if (data != null) {
                List<TushuoItem> items = (List<TushuoItem>) data.getItems();
                if (items == null || items.isEmpty())
                    return;
                // TushuoItem item = items.get(0);
                // if (item == null)
                // return;
                // List<PicModel> picmodels = item.getPicModelList();
                // if (picmodels == null)
                // return;
                mData.removeAll(mData);
                mData.addAll(items);
                mAdapter.notifyDataSetChanged();

                // mBottom.setText(item.getContent());
                updateHead(0);
                updateBottom(0);
                updateUserActionBar(0);
            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public ImagePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Fragment getItem(int position) {

            TushuoItem item = mData.get(position);
            if (item == null)
                return null;

            PicModel picModel = item.getPicModel();
            String url = null;
            if (picModel != null) {
                url = picModel.url;
            }
            ImageDetailFragment instance = ImageDetailFragment.newInstance(url);
            instance.setListener(new ImageDetailFragment.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switchShow();
                }
            });
            return instance;
        }
    }

    public static DetailTushuoFragment newInstance(String cate) {
        final DetailTushuoFragment f = new DetailTushuoFragment();

        final Bundle args = new Bundle();
        args.putString(FLAG_CATE, cate);
        f.setArguments(args);

        return f;
    }

    public static DetailTushuoFragment newInstance(int id) {
        final DetailTushuoFragment f = new DetailTushuoFragment();

        final Bundle args = new Bundle();
        args.putInt(TushuoItem.ID, id);
        f.setArguments(args);

        return f;
    }

}
